/**
 * Copyright (c) 2015-2016, smeooncun 失色 (semooncun@foxmail.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lubiaoqing.jsite.util;

import java.util.ArrayList;
import java.util.List;

import com.lubiaoqing.jsite.model.Menu;

public class AdminMenu {

	public static List<Menu> ADMIN_MENU = new ArrayList<Menu>();

	public static void init() {
		ADMIN_MENU.clear();
		ADMIN_MENU.add(new Menu("", "", "功能菜单", false, "div"));
		ADMIN_MENU.add(new Menu("index", "fa-dashboard", "控制面板", true, "menu"));
		ADMIN_MENU.add(new Menu("", "", "基本设置", false, "div"));
		ADMIN_MENU.add(new Menu("website", "fa-gears", "网站设置", false, "menu"));
		ADMIN_MENU.add(new Menu("nav", "fa-life-bouy", "自定义导航", false, "menu"));
		ADMIN_MENU.add(new Menu("", "", "内容管理", false, "div"));
		ADMIN_MENU
				.add(new Menu("page", "fa-file-code-o", "页面管理", false, "menu"));
		ADMIN_MENU.add(new Menu("content", "fa-clipboard", "文章管理", false,
				"menu"));
		ADMIN_MENU.add(new Menu("msg", "fa-envelope-o", "访客消息", false, "menu"));
		ADMIN_MENU.add(new Menu("", "", "其它", false, "div"));
		ADMIN_MENU.add(new Menu("tpl", "fa-flag-checkered", "模板管理", false,
				"menu"));
		ADMIN_MENU.add(new Menu("backup", "fa-repeat", "数据备份", false, "menu"));
	}

	public static void setActive(String id) {
		if (ADMIN_MENU.size() < 0) {
			init();
		}
		for (int i = 0; i < ADMIN_MENU.size(); i++) {
			if (ADMIN_MENU.get(i).getType().equals("menu")) {
				if (ADMIN_MENU.get(i).getId().equals(id)) {
					ADMIN_MENU.get(i).setActive(true);
				} else {
					ADMIN_MENU.get(i).setActive(false);
				}
			} else {
				continue;
			}
		}
	}

	public static String generateHtml(String cpath) {
		StringBuilder html = new StringBuilder();
		if (ADMIN_MENU.size() < 0) {
			init();
		}
		Menu menu = new Menu();
		for (int i = 0; i < ADMIN_MENU.size(); i++) {
			menu = ADMIN_MENU.get(i);
			if (menu.getType().equals("div")) {
				html.append("<li class='header'>" + menu.getText() + "</li>");
			} else {
				if (menu.isActive()) {
					html.append("<li class='active'>");
				} else {
					html.append("<li>");
				}
				html.append("<a href='" + cpath + "/admin/" + menu.getId()
						+ "'><i class='fa " + menu.getIco() + "'></i><span>"
						+ menu.getText() + "</span></a></li>");
			}
		}
		return html.toString();
	}
}
